class ApplicationAccountsController < ApplicationController
  #before_filter :require_admin_user
  
  # GET /application_accounts
  # GET /application_accounts.xml
  def index
    @application_accounts = ApplicationAccount.paginate :page => params[:page]
    #@application_accounts = ApplicationAccount.all
    @statuses = ApplicationAccount.statuses

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @application_accounts }
    end
  end

  # GET /application_accounts/1
  # GET /application_accounts/1.xml
  def show
    @application_accounts = ApplicationAccount.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @application_accounts }
    end
  end

  # GET /application_accounts/new
  # GET /application_accounts/new.xml
  def new
    @application_accounts = ApplicationAccount.new
    basic_data

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @application_accounts }
    end
  end

  # GET /application_accounts/1/edit
  def edit
    @application_accounts = ApplicationAccount.find(params[:id])
    basic_data
  end

  # POST /application_accounts
  # POST /application_accounts.xml
  def create
    @application_accounts = ApplicationAccount.new(params[:application_account])

    basic_data
    
    respond_to do |format|
      if @application_accounts.save
        flash[:notice] = 'ApplicationAccount was successfully created.'
        format.html { redirect_to(@application_accounts) }
        format.xml  { render :xml => @application_accounts, :status => :created, :location => @application_accounts }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @application_accounts.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /application_accounts/1
  # PUT /application_accounts/1.xml
  def update
    @application_accounts = ApplicationAccount.find(params[:id])
    basic_data

    respond_to do |format|
      if @application_accounts.update_attributes(params[:application_account])
        flash[:notice] = 'ApplicationAccount was successfully updated.'
        format.html { redirect_to(@application_accounts) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @application_accounts.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /application_accounts/1
  # DELETE /application_accounts/1.xml
  def destroy
    @application_accounts = ApplicationAccount.find(params[:id])
    err = @application_accounts.destroy_or_canceled

    respond_to do |format|
      format.html { redirect_to(application_accounts_url) }
      if err
        format.xml  { head :ok }
      else
        flash[:error] = @application_accounts.errors.full_messages.to_s
        format.xml  { render :xml => @application_accounts.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # REGENERATE API KEY
  # ----- temporary until flowchart
  def regenerate_apikey
    @application_accounts = ApplicationAccount.find(params[:id])
    @application_accounts.regenerate_apikey
    
    respond_to do |format|
      flash[:notice] = 'New API Key was successfully generated.'
      format.html { redirect_to(@application_accounts) }
      format.xml  { render :xml => @application_accounts, :status => :created, :location => @application_accounts }
    end  
  end
  # # batch updates statuses
  # def change_statuses
  #   statuses = params[:application_account]
  # end
  
  private
  def basic_data
    @statuses = ApplicationAccount.statuses
    @bundle_variations = BundleVariation.find(:all, :include => [:bundle, :trial_period])
    @bundles = Bundle.find(:all, :include => [:bundle_variations])
    @distributors = Distributor.find(:all)
  end
end
